﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IFTRUE.EIMS.Helper
{
    /// <summary>
    /// 作用：类型转换类
    /// 作者：iftrue
    /// 日期：2013-08-28
    /// </summary>
   public class TypeChange
    {
        /// <summary>
        /// 字符串转换成需要的类型值
        /// </summary>
        /// <typeparam name="T">强制转换的类型</typeparam>
        /// <param name="value">待转换字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static T str2realtype<T>(string value, T defaultValue)
        {
            if (string.IsNullOrEmpty(value))
            {
                return defaultValue;
            }
            try
            {
                return (T)Convert.ChangeType(value, typeof(T));
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// object类型转换成需要的类型值
        /// </summary>
        /// <typeparam name="T">强制转换的类型</typeparam>
        /// <param name="value">待转换object</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static T object2realtype<T>(object value, T defaultValue)
        {
            try
            {
                return (T)Convert.ChangeType(value, typeof(T));
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 16进制字符串转换成Byte类型
        /// </summary>
        /// <param name="input">16进制字符串</param>
        /// <returns></returns>
        public static byte[] string2byte(string input)
        {
            byte[] bt = new byte[input.Length / 2];
            int n = 0;
            for (int i = 0; i < input.Length; i += 2)
            {
                bt[n] = Convert.ToByte(Convert.ToInt32(input.Substring(i, 2), 16));
                n++;
            }
            return bt;
        }

        /// <summary>
        /// Byte类型转换成16进制字符串
        /// </summary>
        /// <param name="input">Byte类型</param>
        /// <returns></returns>
        public static string byte2string(byte[] input)
        {
            StringBuilder sb = new StringBuilder();//建立一个变长数组 
            for (int i = 0; i < input.Length; i++)
            {
                sb.AppendFormat("{0:x2}", input[i]);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 16进制字符串转换成对应编码的字符串
        /// </summary>
        /// <param name="input">16进制字符串</param>
        /// <param name="toEncode">对应编码</param>
        /// <returns></returns>
        public static string strbyte2str(string input, Encoding toEncode)
        {
            byte[] tobyte = string2byte(input);
            return toEncode.GetString(tobyte);
        }

        /// <summary>
        /// Byte类型转换成对应编码字符串
        /// </summary>
        /// <param name="input">Byte类型</param>
        /// <param name="toEncode">对应编码</param>
        /// <returns></returns>
        public static string byte2str(byte[] input, Encoding toEncode)
        {
            return toEncode.GetString(input);
        }

        /// <summary>
        /// 字符串转换成其他编码的16进制字符串
        /// </summary>
        /// <param name="input">字符串转</param>
        /// <param name="fromEncode">源编码</param>
        /// <param name="toEncode">目标编码</param>
        /// <returns></returns>
        public static string encode_str2str16(string input, Encoding fromEncode, Encoding toEncode)
        {
            byte[] frombyte = fromEncode.GetBytes(input.ToCharArray());
            byte[] tobyte = Encoding.Convert(fromEncode, toEncode, frombyte);
            return byte2string(tobyte);
        }

        /// <summary>
        /// 字符串转换成其他编码的字符串
        /// </summary>
        /// <param name="input">字符串转</param>
        /// <param name="fromEncode">源编码</param>
        /// <param name="toEncode">目标编码</param>
        /// <returns></returns>
        public static string encode_str2str(string input, Encoding fromEncode, Encoding toEncode)
        {
            byte[] frombyte = fromEncode.GetBytes(input.ToCharArray());
            byte[] tobyte = Encoding.Convert(fromEncode, toEncode, frombyte);
            return byte2str(tobyte, toEncode);
        }
       
        /// <summary>
        /// 将unicode字符串转换成16进制字符串信息，每个字符前加%
        /// </summary>
        /// <param name="input">unicode字符串</param>
        /// <returns></returns>
        public static string str2str16(string input)
        {
            StringBuilder sb = new StringBuilder();

            byte[] input2byte = Encoding.Unicode.GetBytes(input);
            byte[] tby = Encoding.Convert(Encoding.Unicode,
            Encoding.GetEncoding("GB2312"), input2byte);
            foreach (byte b in tby)
            {
                sb.AppendFormat("%{0:x2}", b);
            }
            return sb.ToString().ToUpper();
        }
    }
}
